package com.example.servlettemplate.dao;

import com.example.servlettemplate.pojo.User;
import com.example.servlettemplate.util.DBUtil;
import static com.example.servlettemplate.util.DBUtil.pstmt;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;



public class UserDAO {

    // 根据用户名查询用户
    public User getUserByUsername(String username) {
        String sql = "select id,username,password,nickname from user where username = ?";
        List param = new ArrayList();
        param.add(username);
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.open();
            stmt = DBUtil.pstmt(sql, param, conn);
            rs = stmt.executeQuery();
            if (rs.next()) {
                // 封装数据到用户对象
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(conn, stmt, rs);
        }
        return null;
    }

    //根据主键查询用户
    public User getUserById(int id) {
        String sql = "select id,username,password,nickname from user where id = ?";
        List param = new ArrayList();
        param.add(id);
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.open();
            stmt = DBUtil.pstmt(sql, param, conn);
            rs = stmt.executeQuery();
            if (rs.next()) {
                // 封装数据到用户对象
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(conn, stmt, rs);
        }
        return null;
    }

    public int add(User user) {
        String sql = "insert into user(username,password,nickname) values(?,?,?)"; //id已经自增，不需要占位符了
        List param = new ArrayList();
        param.add(user.getUsername());
        param.add(user.getPassword());
        param.add(user.getNickname());
        return DBUtil.executeUpdate(sql,param);
    }

    //修改密码以外的信息字段
    public int update(User user) {
        String sql = "update user set nickname=? where id=?";
        List param = new ArrayList();
        param.add(user.getNickname());
        param.add(user.getId());
        return DBUtil.executeUpdate(sql,param);
    }
    //修改密码
    public int updatePassword(int id, String newPassword) {
        String sql = "update user set password=? where id=?";
        List param = new ArrayList();
        param.add(newPassword);
        param.add(id);
        return DBUtil.executeUpdate(sql,param);
    }
}
